package net.huashitong.index.dao;

import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.index.dataModel.IndexTargetDataModel;
import net.huashitong.index.entity.IndexTargetEntity;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class IndexTargetDao extends StringDao<IndexTargetEntity> {

    public List<IndexTargetDataModel> queryByFilter(IndexTargetDataModel filter){
        StringBuffer sql = new StringBuffer();
        //指标目标、指标表、企业表、单位表关联
        sql.append("" +
                "select \n" +
                "t2.sys_id as sysId,\n" +                   //目标表主键
                "t1.date_unit as dateUnit,\n" +                 //时间
                "t1.datetime as datetime,\n" +                  //指标年份
                "t1.index_level as indexLevel,\n" +                 //指标等级
                "t1.index_name as indexName,\n" +                   //指标名称
                "t1.leader as leader,\n" +                  //分管领导
                "t1.sort as sort,\n" +                  //排序
                "t1.parent_index_id as parentIndexId,\n" +                  //父级指标编码
                "t1.match_dept as matchDept,\n" +                   //匹配单位
                "t1.resp_dept as respDept,\n" +                 //责任单位
//                "d3.dept_name as source,\n" +                   //来源单位
                "t1.source as source,\n" +                   //来源单位
                "d2.dept_name as town,\n" +                 //乡镇
                "e1.enterprise_name as enterpriseName,\n" +                 //企业
                "d1.dept_name as leadDept,\n" +                 //牵头单位
                "t2.index_state as indexState,\n" +                 //指标状态
                "t2.target_time as targetTime,\n" +                 //目标时间
                "t2.target_value as targetValue,\n" +                   //目标值
                "t2.value_type as valueType\n" +                    //目标值类型
                "from \n" +
                "t_eco_index t1\n" +
                "INNER JOIN t_eco_depart d1 ON t1.lead_dept = d1.dept_code\n" +
                "LEFT JOIN t_eco_depart d2 ON t1.town = d2.dept_code\n" +
//                "LEFT JOIN t_eco_depart d3 ON t1.source = d3.dept_code\n" +
                "INNER JOIN t_eco_indextarget t2 ON t1.sys_id = t2.index_id \n" +
                "LEFT JOIN t_eco_etprise e1 ON t1.enterprise = e1.sys_id where 1 = 1 ");

        if(ObjectUtils.isNotBlank(filter.getSource())){
            sql.append("and t1.source = '" + filter.getSource()+"' " );
        }
        if(ObjectUtils.isNotBlank(filter.getSysId())){

        }
        if(ObjectUtils.isNotBlank(filter.getIndexId())){

        }
        if(ObjectUtils.isNotBlank(filter.getTargetValue())){

        }
        if(ObjectUtils.isNotBlank(filter.getValueType())){

        }
        if(ObjectUtils.isNotBlank(filter.getTargetTime())){

        }
        if(ObjectUtils.isNotBlank(filter.getDateUnit())){

        }
        if(ObjectUtils.isNotBlank(filter.getIndexState())){

        }
        if(ObjectUtils.isNotBlank(filter.getParentIndexId())){

        }
        if(ObjectUtils.isNotBlank(filter.getIndexName())){

        }
        if(ObjectUtils.isNotBlank(filter.getDatetime())){

        }
        if(ObjectUtils.isNotBlank(filter.getIndexLevel())){

        }
        if(ObjectUtils.isNotBlank(filter.getLeadDept())){

        }
        if(ObjectUtils.isNotBlank(filter.getRespDept())){

        }
        if(ObjectUtils.isNotBlank(filter.getMatchDept())){

        }
        if(ObjectUtils.isNotBlank(filter.getEnterpriseId())){

        }
        if(ObjectUtils.isNotBlank(filter.getEnterpriseName())){

        }
        if(ObjectUtils.isNotBlank(filter.getTown())){

        }
        if(ObjectUtils.isNotBlank(filter.getLeader())){

        }
        if(ObjectUtils.isNotBlank(filter.getSource())){

        }
        //关联表做分页处理
        if (filter.getPaging() != null) {
            if (filter.getPaging().getSortCol() == null || "".equals(filter.getPaging().getSortCol())) {
                sql.append(" ORDER BY  str_to_date( CONCAT(t1.upd_date,' ',t1.upd_time),'%Y-%m-%d %H:%i:%s') desc  ");
            }
        }

        String hql = sql.toString();
        hql = new StringBuffer(" select count(1) ").append(" from ( ")
                .append(hql).append(" ) temp ").toString();
        Query getCount = getSession().createSQLQuery(hql);
        if (filter.getPaging() != null) {
            int count = ((Number) getCount.uniqueResult()).intValue();
            int resultSize = count;
            int size = filter.getPaging().getPageSize();
            int page = filter.getPaging().getCurrentPage();
            int MaxPage = resultSize / size;
            if (!(resultSize % size == 0)) {
                MaxPage = (resultSize / size) + 1;
            }
            if (page > MaxPage) {
                filter.getPaging().setCurrentPage(MaxPage);
            }
            filter.getPaging().setRecords(resultSize);
        }

        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(IndexTargetDataModel.class));
        if (filter.getPaging() != null) {
            query.setFirstResult(filter.getPaging().getStartRow());
            query.setMaxResults(filter.getPaging().getPageSize());
        }
        List<IndexTargetDataModel> result = query.list();
        return  result;
    }
}
